using Microsoft.AspNetCore.Mvc;
using MediatR;
using Microsoft.Extensions.Logging;
using RBAC.ErrorCode;
using RBAC.Read.Api.Application.Command.ArrivalInspection;
using RBAC.Read.Api.Application.Dto.Quality;

namespace RBAC.Read.Api.Controllers
{
    /// <summary>
    /// 到货检验控制器
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class ArrivalInspectionController : ControllerBase
    {
        private readonly IMediator _mediator;
        private readonly ILogger<ArrivalInspectionController> _logger;

        public ArrivalInspectionController(IMediator mediator, ILogger<ArrivalInspectionController> logger)
        {
            _mediator = mediator;
            _logger = logger;
        }

        /// <summary>
        /// 到货检验主表分页查询（条件可选）
        /// </summary>
        [HttpGet]
        public Task<APIPaging<ArrivalInspectionListDto>> GetList([FromQuery] GetArrivalInspectionListCommand request, CancellationToken cancellationToken)
        {
            return _mediator.Send(request, cancellationToken);
        }

        /// <summary>
        /// 导出到货检验（CSV）。
        /// - 若传入 Id：导出该单据的明细（含部分单据头字段）。
        /// - 未传入 Id：导出符合条件的单据列表（含汇总列）。
        /// </summary>
        [HttpGet]
        public async Task<IActionResult> Export([FromQuery] ExportArrivalInspectionCommand request, CancellationToken cancellationToken)
        {
            var bytes = await _mediator.Send(request, cancellationToken);
            var fileName = $"ArrivalInspection_{DateTime.Now:yyyyMMddHHmmss}.csv";
            return File(bytes, "text/csv; charset=utf-8", fileName);
        }
    }
}
